package com.dogborther.controller;

import com.dogborther.LemonApplication;
import com.dogborther.pojo.Chopstick;
import com.dogborther.pojo.Mode2Philosopher;
import com.dogborther.pojo.Philosopher;
import com.dogborther.utils.ChopstickStatus;
import com.dogborther.utils.PhilosopherCreateFactory;
import com.dogborther.utils.PhilosopherStatus;
import com.dogborther.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@FXMLController
public class MainViewController implements Initializable {

    private AnchorPane mainRootPane;
    @FXML
    public AnchorPane lockModeVersion0;
    @FXML
    public AnchorPane unlockModeVersion1;
    @FXML
    public AnchorPane unlockModeVersion2;
    @FXML
    public AnchorPane unlockModeVersion3;
    @Autowired
    private MenuView menuView;

    @FXML
    private Button eatStart;
    @FXML
    private Button back;
    private static boolean isPlay = true;
    private Philosopher[] philosophers;
    private Chopstick[] chopsticks;
    private static Integer selectMode = 0;

    public static void setIsPlay(boolean isPlay) {
        MainViewController.isPlay = isPlay;
    }

    public static void setSelectMode(Integer selectMode) {
        MainViewController.selectMode = selectMode;
    }

    private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(1000);

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (selectMode == 0) {
            mainRootPane = lockModeVersion0;
        } else if (selectMode == 1) {
            mainRootPane = unlockModeVersion1;
        } else if (selectMode == 2) {
            mainRootPane = unlockModeVersion2;
        } else if (selectMode == 3) {
            mainRootPane = unlockModeVersion3;
        }
        chopsticks = new Chopstick[5];
        for (int i = 0; i < 5; i++) {
            chopsticks[i] = new Chopstick();
            chopsticks[i].setImageView((ImageView) mainRootPane.lookup("#c" + i));
            chopsticks[i].setImage(ChopstickStatus.DROP_DOWN);
        }
        philosophers = new Philosopher[5];
        for (int i = 0; i < 5; i++) {
            philosophers[i] = PhilosopherCreateFactory.getPhilosopherBySelectMode(selectMode);
            philosophers[i].setId(i);
            philosophers[i].setLeftHandChopstick((ImageView) mainRootPane.lookup("#p" + i + "l"));
            philosophers[i].setRightHandChopstick((ImageView) mainRootPane.lookup("#p" + i + "r"));
            philosophers[i].setLeftChopstick(chopsticks[i]);
            philosophers[i].setRightChopstick(chopsticks[(i + 5 - 1) % 5]);
            philosophers[i].setImageView((ImageView) mainRootPane.lookup("#p" + i));
            philosophers[i].setImage(PhilosopherStatus.THINKING);
        }
        eatStart.setOnAction(e -> {
            isPlay=true;
            play();
        });
        back.setOnAction(e -> {
            isPlay=false;
        });

    }

    public void play() {
        EXECUTOR.submit(() -> {
            while (isPlay) {
                philosophers[0].run();
            }
        });
        EXECUTOR.submit(() -> {
            while (isPlay) {
                philosophers[1].run();
            }
        });
        EXECUTOR.submit(() -> {
            while (isPlay) {
                philosophers[2].run();
            }
        });
        EXECUTOR.submit(() -> {
            while (isPlay) {
                philosophers[3].run();
            }
        });
        EXECUTOR.submit(() -> {
            while (isPlay) {
                philosophers[4].run();
            }
        });
    }
}
